package com.example.explainus;

import com.example.units.GameInfo;
import com.example.units.GlobalParameters;
import com.example.units.GlobalVariables;
import com.example.units.Team;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

public class NewGameActivity extends Activity 
{

	private static GameInfo roundInfo;
	private boolean bTeam1Pressed = false;
	private boolean bTeam2Pressed = false;
	private boolean bTeam3Pressed = false;
	private boolean bTeam4Pressed = false;
	private boolean isOk = false;
	private Button bSettings;
	private Button bTime1;
	private Button bTime2;
	private Button bTime3;
	private Button bTime4;
	private Button bBack;
	private Button bStart;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		roundInfo = new GameInfo();
		setContentView(R.layout.activity_newgame);
		isOk = false;
		GlobalParameters.overrideFonts(this, findViewById(R.id.textView1));
		GlobalParameters.overrideFonts(this, findViewById(R.id.textView2));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bngStart));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bngBack));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTime30s));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTime60s));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTime90s));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTime120s));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTeam1));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTeam2));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTeam3));
		GlobalParameters.overrideFonts(this, findViewById(R.id.bTeam4));
		bSettings = (Button)findViewById(R.id.bRoundSettings);
		bBack = (Button)findViewById(R.id.bngBack);
		bStart = (Button)findViewById(R.id.bngStart);
		bBack.setBackgroundResource(R.drawable.button_boarder);
		bStart.setBackgroundResource(R.drawable.button_boarder);
		bTime1 = (Button)findViewById(R.id.bTime30s);
		bTime2 = (Button)findViewById(R.id.bTime60s);
		bTime3 = (Button)findViewById(R.id.bTime90s);
		bTime4 = (Button)findViewById(R.id.bTime120s);
		roundInfo.setRoundTime(30);
	}
	
	
	public void goSettings(View v)
	{
		try
		{
			Intent start = new Intent(NewGameActivity.this, GameSettingsActivity.class);
			startActivity(start);
			finish();
		}
		catch(Exception e)
		{
			e.getStackTrace();
		}
	}
	
	private void buttonLickRatio(int number)
	{
		switch(number)
		{
			case 1: 
			{
				bTime1.setBackgroundResource(R.drawable.button_boarder_mini_change);
				bTime2.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime3.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime4.setBackgroundResource(R.drawable.button_boarder_mini);
			}
			break;
			case 2: 
			{
				bTime1.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime2.setBackgroundResource(R.drawable.button_boarder_mini_change);
				bTime3.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime4.setBackgroundResource(R.drawable.button_boarder_mini);
			}
			break;
			case 3: 
			{
				bTime1.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime2.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime3.setBackgroundResource(R.drawable.button_boarder_mini_change);
				bTime4.setBackgroundResource(R.drawable.button_boarder_mini);
			}
			break;
			case 4: 
			{
				bTime1.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime2.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime3.setBackgroundResource(R.drawable.button_boarder_mini);
				bTime4.setBackgroundResource(R.drawable.button_boarder_mini_change);
			}
			break;
			case 5: 
			{
				bStart.setBackgroundResource(R.drawable.button_boarder_change);
				bBack.setBackgroundResource(R.drawable.button_boarder);
			}
			break;
			case 6: 
			{
				bStart.setBackgroundResource(R.drawable.button_boarder);
				bBack.setBackgroundResource(R.drawable.button_boarder_change);
			}
			break;
			default : break;
		}
	}
	
	
	/*Buttons onClick Events Region*/
	
	/*bTeam#, bTimeXs events: addTeam#, addTimeX, goBack, goStart*/
	
	public void goBack(View view) 
	{
		buttonLickRatio(6);
		if (GlobalVariables.info != null)
			GlobalVariables.info.eraseAll();
		Intent backStep = new Intent(NewGameActivity.this, MainActivity.class);
		startActivity(backStep);
		finish();
	}
	
	public void addTeam1 (View view)
	{
		if (!this.bTeam1Pressed)
		{
			Team team = new Team(0,"");
			roundInfo.addTeam(0, team);
			Button btn = (Button)findViewById(R.id.bTeam1);
			btn.setBackgroundResource(R.drawable.bteam1_2);
			btn.setText("");
			this.bTeam1Pressed = true;
			
		}
		else
		{
			roundInfo.eraseTeam(0);
			Button btn = (Button)findViewById(R.id.bTeam1);
			btn.setText(getResources().getString(R.string.bnumberteam1));
			btn.setBackgroundResource(R.drawable.button_boarder_mini);
			this.bTeam1Pressed = false;
		}
	}
	
	public void addTeam2 (View view)
	{
		if (!this.bTeam2Pressed)
		{
			Team team = new Team(0,"");
			roundInfo.addTeam(1, team);
			Button btn = (Button)findViewById(R.id.bTeam2);
			btn.setBackgroundResource(R.drawable.bteam2_2);
			btn.setText("");
			this.bTeam2Pressed = true;
			
		}
		else
		{
			roundInfo.eraseTeam(1);
			Button btn = (Button)findViewById(R.id.bTeam2);
			btn.setText(getResources().getString(R.string.bnumberteam2));
			btn.setBackgroundResource(R.drawable.button_boarder_mini);
			this.bTeam2Pressed = false;
		}
	}
	
	public void addTeam3 (View view)
	{
		if (!this.bTeam3Pressed)
		{
			Team team = new Team(0,"");
			roundInfo.addTeam(2, team);
			Log.d("DEBUG", "Button team1 presed");
			Button btn = (Button)findViewById(R.id.bTeam3);
			btn.setBackgroundResource(R.drawable.bteam3_2);
			btn.setText("");
			this.bTeam3Pressed = true;
			
		}
		else
		{
			roundInfo.eraseTeam(2);
			Button btn = (Button)findViewById(R.id.bTeam3);
			btn.setText(getResources().getString(R.string.bnumberteam3));
			btn.setBackgroundResource(R.drawable.button_boarder_mini);
			this.bTeam3Pressed = false;
		}
	}
	
	public void addTeam4 (View view)
	{
		if (!this.bTeam4Pressed)
		{
			Team team = new Team(0,"");
			roundInfo.addTeam(3, team);
			Button btn = (Button)findViewById(R.id.bTeam4);
			btn.setBackgroundResource(R.drawable.bteam4_2);
			btn.setText("");
			this.bTeam4Pressed = true;
			
		}
		else
		{
			roundInfo.eraseTeam(3);
			Button btn = (Button)findViewById(R.id.bTeam4);
			btn.setText(getResources().getString(R.string.bnumberteam4));
			btn.setBackgroundResource(R.drawable.button_boarder_mini);
			this.bTeam4Pressed = false;
		}
	}
	
	public void addTime30 (View view)
	{
		roundInfo.setRoundTime(30);
		Log.d("DEBUG", "Button time30 presed");
		buttonLickRatio(1);
	}
	
	public void addTime60 (View view)
	{
		roundInfo.setRoundTime(60);
		buttonLickRatio(2);
	}
	
	public void addTime90 (View view)
	{
		roundInfo.setRoundTime(90);
		buttonLickRatio(3);
	}
	
	public void addTime120 (View view)
	{
		roundInfo.setRoundTime(120);
		buttonLickRatio(4);
	}
	
	
	public void goStart (View view)
	{
		try
		{
			buttonLickRatio(5);
			if (roundInfo.getTeamsCount() >= 2)
			{
				Intent start = new Intent(NewGameActivity.this, PrepareRoundActivity.class);
				GlobalVariables.info = new GameInfo();
				GlobalVariables.info = roundInfo;
				GlobalVariables.gameNow = GlobalVariables.info.getFirstTeam();
				startActivity(start);
				isOk = true;
			}
			else
			{
				isOk = false;
				AlertDialog.Builder warningAlert = new AlertDialog.Builder(this);
				warningAlert.setTitle(getResources().getString(R.string.tdialogtitle));
				warningAlert.setMessage(getResources().getString(R.string.twarningngdialog));
				warningAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
											
					}
				});
				
				AlertDialog alert = warningAlert.create();
				alert.show();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally 
		{
			if (isOk)
				finish();
		}
	}
	
	
	
	/*End Buttons onClick Events Region*/

}
